package com.briup.web.listener;

import com.briup.bean.Book;
import com.briup.bean.Category;
import com.briup.service.IBookService;
import com.briup.service.ICategoryService;
import com.briup.service.impl.BookServiceImpl;
import com.briup.service.impl.CategoryServiceImpl;

import javax.servlet.*;
import javax.servlet.annotation.WebListener;
import java.util.List;

@WebListener
public class IndexListener implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent sce) {
//        1、获取servletContext对象
        ServletContext servletContext = sce.getServletContext();
        ICategoryService categoryService = new CategoryServiceImpl();
        IBookService bookService = new BookServiceImpl();
        List<Category> allCategorys = categoryService.findAllCategorys();
//        2、填空是把查询结果存到最大的作用域当中
        servletContext.setAttribute("CategoryList",allCategorys);
//        xxx.setAttribute("CategoryList",?);

        //查询图书
        List<Book> allBooks = bookService.findAllBooks();
        //存入对应作用域
        servletContext.setAttribute("BookList",allBooks);
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {

    }
}
